/*
 * 打开或创建文件夹,创建文件并写入内容
 * Android:sdcard/xbrother/assets目录
 * IOS:cdvfile://localhost/persistent/xbrother/assets目录
 * 文件目录存在则打开,不存在则创建
 * */
export function createAndWriteFile(filename, data) {
    window.resolveLocalFileSystemURL(window.cordova.file.externalApplicationStorageDirectory, function(directoryEntry) {
        directoryEntry.getFile(filename, { create: true }, function(fileEntry) {
            var dataObj = new Blob([data], { type: 'text/plain' });
            writeFile(fileEntry, dataObj);
        }, onErrorCreateFile);
    }, onErrorLoadFs);
}

// Write data to the file
function writeFile(fileEntry, dataObj) {
    fileEntry.createWriter(function(fileWriter) {
        fileWriter.onwriteend = function() {
            console.log("Successful file write...");
        };

        fileWriter.onerror = function(e) {
            console.log("Failed file write: " + e.toString());
        };

        fileWriter.write(dataObj);
    });
}



//FileSystem加载失败回调
function onErrorLoadFs(error) {
    console.log("文件系统加载失败！" + error)
    console.log(error)
}


//文件创建失败回调
function onErrorCreateFile(error) {
    console.log("文件创建失败！")
    console.log(error)
}
